<?php

class SpeSubAction extends STrendsAction {
	public function add(){
		if (isset($_SESSION[C('USER_AUTH_KEY')]))
			$this->display();
		else
			$this->redirect('Public/login');
	}
	public function insert(){
		
		import("ORG.UploadFile");
		import("ORG.FileUtil");
		require(STRENDS_ROOT.'/core/function/global.func.php');
		$upload = new UploadFile(); 
		$fileutil = new FileUtil(); 
		$upload->savePath =  './public/';
		$upload->maxSize  = 204800000; // 设置附件上传大小
		$upload->saveRule = uniqid;
		if(!$upload->upload()) { // 上传错误提示错误信息
			echo 'error'; exit;
			//unset($data['full']);
		}else{ 
			$info =  $upload->getUploadFileInfo();
			foreach($info as $vo){
				$filepath = $vo['savepath'].$vo['savename'];
				$filename = $vo['name'];
			}
		}
		
		$dirname = explode('.',$filename);
		pclZipFun($filepath,'./public',true);
		$webpath = $_POST['speweb'];
		
		$path = './public/'.$dirname[0];
		$this->getfiles($path,$webpath);
		if(!is_dir($path)) return;
		$handle  = opendir($path);
		while( false !== ($file = readdir($handle)))
		{
			if($file != '.' && $file!='..'){
				$path2= $path.'/'.$file;
				if($file!='css' && $file!='js' && $file!='images'){
					$fileutil->moveDir($path2, RES_ROOT.'/minisite/'.$webpath.'/'.$file);
				}else{
					$handle  = opendir($path);
					while( false !== ($file = readdir($handle)))
					{
						if($file != '.' && $file!='..'){
							$path2 = $path.'/'.$file;
							$fileutil->moveDir($path2,RES_ROOT.'/minisite/'.$webpath.'/'.$file);
						}
					}
				}
			}
		}
		$fileutil->unlinkDir($path);
		echo "<script>var i=1;function addnum(){if(i<4){document.getElementById('turnnum').innerHTML=i;}else{location.href='index.php?m=SpeSub&a=add';}i++;} if(i<5){setInterval('addnum()',1000)}</script>
		<div style='width:100px;margin-left:300px;margin-top:200px;padding:20px 30px;border:2px solid green;'>
		<a href='index.php?m=SpeSub&a=add'>上传成功</a><span style='margin-left:10px;' id='turnnum'>0</span></div>";
		//$this->display('SpeSub/add');
	}
	public function getfiles($path,$webpath){
		if(!is_dir($path)) return;
		$handle  = opendir($path);
		while( false !== ($file = readdir($handle)))
		{
			if($file != '.' && $file!='..' && $file!='images')
			{
				$path2= $path.'/'.$file;
				if(is_dir($path2))
				{
				  $this->getfiles($path2,$webpath);
				}else{
					$str = file_get_contents($path2);
					//echo $str;
					iconv("GB2312","UTF-8",$str); 
					$arr = explode('/',$path2);
					
					$filestr = str_replace('../',"/minisite/".$webpath."/",$str);
					file_put_contents($path2,$filestr);
					
				}
			}
		}
	} 
}
?>